package org.example.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.entity.Files;
import org.example.mapper.FolderMapper;
import org.example.mapper.NoteMapper;
import org.example.service.FilesService;
import org.example.mapper.FilesMapper;
import org.example.service.NoteService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
* @author ABin
* @description 针对表【files】的数据库操作Service实现
* @createDate 2024-04-03 23:32:06
*/
@Service
public class FilesServiceImpl extends ServiceImpl<FilesMapper, Files>
    implements FilesService{

    @Resource
    private FilesMapper filesMapper;

    @Resource
    private FolderMapper folderMapper;

    @Resource
    private NoteMapper noteMapper;



    @Override
    public List<Files> search(String keyword, String startTime, String endTime, Long userId) {
        return filesMapper.search(keyword, startTime, endTime, userId);
    }

    @Override
    public String fileNameExistsReset(Map<String, Object> file) {
        String fileType = (String) file.get("fileType");
        if(fileType == null || "".equals(fileType)){
            throw new RuntimeException("文件类型不能为空");
        }
        String fileName = (String) file.get("fileName");
        Long userId = Long.valueOf(file.get("userId").toString());
        String newFileName = fileName;
        boolean isExist = true;
        int fileSuffix = 0;
        while(isExist){
            switch (fileType) {
                case "folder":
                    isExist = folderMapper.search(newFileName, null, null, userId).size() > 0;
                    break;
                case "note":
                    isExist = noteMapper.search(newFileName, null, null, null, userId).size() > 0;
                    break;
                case "file":
                    isExist = filesMapper.search(newFileName, null, null, userId).size() > 0;
                    break;
                default:
                    throw new RuntimeException("文件类型错误");
            }
            if(isExist){
                fileSuffix++;
                newFileName = fileName + "(" + fileSuffix + ")";
            }
        }
        return newFileName;
    }
}




